package com.tourism.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author z 自增id工具类
 * @Date 2021-03-01 20:55
 */
public class IdUtils {

    private static SnowFlake snowFlake = new SnowFlake(0, 0);

    public static long newId() {
        return snowFlake.nextId();
    }

    public static void main(String[] args) {
        String sql = "CREATE TABLE `attraction_evaluation` (\n" +
                "  `id` bigint(11) NOT NULL COMMENT '景点订单评价表',\n" +
                "  `attraction_order_id` bigint(20) DEFAULT NULL COMMENT '景点订单id',\n" +
                "  `attraction_id` bigint(20) DEFAULT NULL COMMENT '景点id',\n" +
                "  `ticket_name` varchar(50) DEFAULT NULL COMMENT '门票名字',\n" +
                "  `evaluation_content` text COMMENT '评价内容',\n" +
                "  `star` int(10) DEFAULT NULL COMMENT '星级',\n" +
                "  `evaluation_time` datetime DEFAULT NULL COMMENT '评价时间',\n" +
                "  `evaluation_user` bigint(20) DEFAULT NULL COMMENT '评价人',\n" +
                "  `create_user` varchar(255) DEFAULT NULL COMMENT '创建人',\n" +
                "  `create_time` datetime DEFAULT NULL COMMENT '创建时间',\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;\n" +
                "\n";

        List<String> data = new ArrayList<>();
        for (int i = 0, len = sql.length(); i < len; i++) {
            if (sql.charAt(i) == '`') {
                StringBuilder word = new StringBuilder();
                for (int j = i + 1; j < len; j++) {
                    if (sql.charAt(j) == '`') {
                        i = j;
                        break;
                    }
                    word.append(sql.charAt(j));
                }
                data.add(word.toString());

            }
        }

//        data.remove(1);
        data.remove(data.size() - 1);

        StringBuilder insertSql = new StringBuilder(String.format("insert into %s(", data.get(0)));
        data.remove(0);
        for (String dd : data) {
            insertSql.append(dd).append(",");
        }
        insertSql.deleteCharAt(insertSql.length() - 1);
        insertSql.append(") values(");
        StringBuilder jsonData = new StringBuilder("{");

        for (String dd : data) {
            insertSql.append("#{").append(toCamelStr(dd)).append("}").append(",");
        }
        data.remove(0);
        for (String dd : data) {
            jsonData.append("\"").append(toCamelStr(dd)).append("\"").append(":,");
        }
        jsonData.deleteCharAt(jsonData.length() - 1);
        jsonData.append("}");
        insertSql.deleteCharAt(insertSql.length() - 1);
        insertSql.append(")");
        System.out.println(insertSql);
        System.out.println(jsonData);

    }

    private static String toCamelStr(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0, size = str.length(); i < size; i++) {
            if (str.charAt(i) == '_') {
                i++;
                sb.append((str.charAt(i) + "").toUpperCase());
            } else {
                sb.append(str.charAt(i));
            }
        }

        return sb.toString();
    }
}
